package com.blog.common.domain.adapter.impl;


import com.blog.common.domain.adapter.ConvertAdapter;
import com.blog.common.domain.annotation.Convert;
import com.blog.common.domain.enums.ConvertEnums;
import com.blog.common.redis.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Objects;
import java.util.Optional;

import static com.blog.common.core.constant.CacheConstants.FILE_INFO;

/**
 * <p>
 * 图片转换
 * </p>
 *
 * @author Valerie
 * @date 2023/5/28
 **/
@Component
public class FileConvertImpl implements ConvertAdapter {
    @Resource
    private RedisService redisService;


    @Override
    public boolean getConvertAdapter(Convert convert) {
        return ConvertEnums.determineAdapter(convert.value(), this.getClass());
    }

    @Override
    public String convertField(Convert convert, String field) {
        boolean flag = Boolean.TRUE.equals(threadLocal.get());
        final String key = FILE_INFO;
        if (!redisService.hasKey(key)){
            return "";
        }
        return flag ? field : (String) Optional.ofNullable(redisService.hGet(key, field)).orElse("");
    }

}